1 using UnityEngine;
2
3 namespace ProceduralToolkit
4 {
5 /// <summary>
6 /// Color extensions
7 /// </summary>
8 public static class ColorE
9 {
10 #region HTML colors from http://www.w3.org/TR/REC-html40/types.html#h-6.5
11
12 public static Color32 black32 { get { return new Color32(0, 0, 0, 255); } }
13 public static Color32 silver32 { get { return new Color32(192, 192, 192, 255); } }
14 public static Color32 gray32 { get { return new Color32(128, 128, 128, 255); } }
15 public static Color32 white32 { get { return new Color32(255, 255, 255, 255); } }
16 public static Color32 maroon32 { get { return new Color32(128, 0, 0, 255); } }
17 public static Color32 red32 { get { return new Color32(255, 0, 0, 255); } }
18 public static Color32 purple32 { get { return new Color32(128, 0, 128, 255); } }
19 public static Color32 fuchsia32 { get { return new Color32(255, 0, 255, 255); } }
20 public static Color32 green32 { get { return new Color32(0, 128, 0, 255); } }
21 public static Color32 lime32 { get { return new Color32(0, 255, 0, 255); } }
22 public static Color32 olive32 { get { return new Color32(128, 128, 0, 255); } }
23 public static Color32 yellow32 { get { return new Color32(255, 255, 0, 255); } }
24 public static Color32 navy32 { get { return new Color32(0, 0, 128, 255); } }
25 public static Color32 blue32 { get { return new Color32(0, 0, 255, 255); } }
26 public static Color32 teal32 { get { return new Color32(0, 128, 128, 255); } }
27 public static Color32 aqua32 { get { return new Color32(0, 255, 255, 255); } }
28
29 public static Color black { get { return black32; } }
30 public static Color silver { get { return silver32; } }
31 public static Color gray { get { return gray32; } }
32 public static Color white { get { return white32; } }
33 public static Color maroon { get { return maroon32; } }
34 public static Color red { get { return red32; } }
35 public static Color purple { get { return purple32; } }
36 public static Color fuchsia { get { return fuchsia32; } }
37 public static Color green { get { return green32; } }
38 public static Color lime { get { return lime32; } }
39 public static Color olive { get { return olive32; } }
40 public static Color yellow { get { return yellow32; } }
41 public static Color navy { get { return navy32; } }
42 public static Color blue { get { return blue32; } }
43 public static Color teal { get { return teal32; } }
44 public static Color aqua { get { return aqua32; } }
45
46 #endregion Colors
47
48 /// <summary>
49 /// Returns inverted color with the same alpha
50 /// </summary>
51 public static Color Inverted(this Color color)
52 {
53 var result = Color.white - color;
54 result.a = color.a;
55 return result;
56 }
57
58 /// <summary>
59 /// Creates a gradient between two colors
60 /// </summary>
61 public static Gradient Gradient(Color from, Color to)
62 {
63 var g = new Gradient();
64 g.SetKeys(new[] {new GradientColorKey(from, 0), new GradientColorKey(to, 1)},
65 new[] {new GradientAlphaKey(from.a, 0), new GradientAlphaKey(to.a, 1)});
66 return g;
67 }
68
69 /// <summary>
70 /// Creates a gradient between two colors
71 /// </summary>
72 public static Gradient Gradient(ColorHSV from, ColorHSV to)
73 {
74 var g = new Gradient();
75 g.SetKeys(new[] {new GradientColorKey(from.ToColor(), 0), new GradientColorKey(to.ToColor(), 1)},
76 new[] {new GradientAlphaKey(from.a, 0), new GradientAlphaKey(to.a, 1)});
77 return g;
78 }
79
80 /// <summary>
81 /// Returns new color with modified red component
82 /// </summary>
83 public static Color WithR(this Color color, float r)
84 {
85 return new Color(r, color.g, color.b, color.a);
86 }
87
88 /// <summary>
89 /// Returns new color with modified green component
90 /// </summary>
91 public static Color WithG(this Color color, float g)
92 {
93 return new Color(color.r, g, color.b, color.a);
94 }
95
96 /// <summary>
97 /// Returns new color with modified blue component
98 /// </summary>
99 public static Color WithB(this Color color, float b)
100 {
101 return new Color(color.r, color.g, b, color.a);
102 }
103
104 /// <summary>
105 /// Returns new color with modified alpha component
106 /// </summary>
107 public static Color WithA(this Color color, float a)
108 {
109 return new Color(color.r, color.g, color.b, a);
110 }
111
112 /// <summary>
113 /// Returns the color as a hexadecimal string in the format "RRGGBB"
114 /// </summary>
115 public static string ToHtmlStringRGB(this Color color)
116 {
117 return ColorUtility.ToHtmlStringRGB(color);
118 }
119
120 /// <summary>
121 /// Returns the color as a hexadecimal string in the format "RRGGBBAA"
122 /// </summary>
123 public static string ToHtmlStringRGBA(this Color color)
124 {
125 return ColorUtility.ToHtmlStringRGBA(color);
126 }
127 }
128 }